package com.taa.controller;

import com.taa.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RefreshScope
@RestController
@RequestMapping("/cart")
public class ProductController {

    @Value("${products.default.discount:0}") // 从配置中心读取折扣率
    private Double discountRate;

    private List<Product> getOriginalProducts() {
        return Arrays.asList(
                new Product(1L, "iPhone 15", 7999.0),
                new Product(2L, "MacBook Pro", 12999.0)
        );
    }

    @GetMapping("/products")
    public List<Product> listProducts() {
        return getOriginalProducts().stream()
                .map(p -> new Product(
                        p.getId(),
                        p.getName(),
                        p.getPrice() * (1 - discountRate) // 应用动态折扣
                ))
                .collect(Collectors.toList());
    }
}

